<template>
  <div class="condition_setting">
    <el-popover popper-class="addrule_el_popover"
                placement="right-start"
                width="1100"
                @hide="resetForm"
                trigger="manual"
                v-model="visible">
      <div class="add_title">{{$t('custom.component.gadget.addRule')}}
        <i class="el-icon-close add_close"
           @click="resetForm"></i>
      </div>
      <el-form :model="addRule"
               :rules="addRuleRules"
               ref="newAddRuleForm"
               style="margin-top:-10px">
        <div style="height: 400px;overflow: auto;">
          <el-form-item :label="$t('custom.component.gadget.widgetRule')"
                        prop="name"
                        style="margin-bottom: 22px;">
            <el-input style="width:100%"
                      v-model="addRule.name"
                      :placeholder="$t('custom.component.gadget.ruleName')"></el-input>
          </el-form-item>
          <el-divider></el-divider>
          <el-form-item prop="dataForm">
            <el-alert :title="$t('custom.component.gadget.timeAlert')"
                      type="warning"
                      class="el-icon-warning"
                      style="margin-bottom:10px;font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif, element-icons!important;"
                      :closable="false">
            </el-alert>
            <TypeFilter :dataForm='addRule.dataForm'
                        :allValueLists="allValueLists"
                        :isConditionFormat='true'
                        :dataInfo="dataInfoCondition"
                        :conditionFieldList="conditionFieldList"
                        :originDataId='dataInfoCondition.gadgetId'
                        :fromDataType='`nextticket`'
                        :originDataType='`widget`'
                        :scriptItem='scriptItem'
                        ref="addtypeFilter"
                        @update='updateData'>
            </TypeFilter>
          </el-form-item>
          <el-divider></el-divider>
          <el-form-item prop="entireRow">
            <div style="display:flex;align-items:center">
              <el-radio v-model="addRule.entireRow"
                        label="cell">Color Cell</el-radio>
              <el-radio v-model="addRule.entireRow"
                        label="row">Color Entire Row</el-radio>
            </div>
          </el-form-item>
          <el-form-item prop="backgroundColor">
            <div style="display:flex;align-items:center">
              <el-color-picker size="small"
                              v-model="addRule.backgroundColor"
                              color-format="hex"
                              :predefine="predefineColors"></el-color-picker>
              <span style="font-size: 14px;font-weight: 400;color: rgba(0, 0, 0, 0.75);margin-left:5px">{{$t('custom.component.gadget.bgColor')}}</span>
            </div>
          </el-form-item>
          <el-form-item>
            <div style="display:flex;align-items:center">
              <el-color-picker size="small"
                              v-model="addRule.textColor"
                              color-format="hex"
                              :predefine="predefineColors"></el-color-picker>
              <span style="font-size: 14px;font-weight: 400;color: rgba(0, 0, 0, 0.75);margin-left:5px">{{$t('custom.component.gadget.textColor')}}</span>
            </div>
          </el-form-item>
          <el-divider></el-divider>
        </div>
        <el-form-item>
          <el-button type="primary"
                     style="float:right"
                     size="mini"
                     @click="isEditModel?edit():add()">{{$t('custom.component.gadget.apply')}}</el-button>
        </el-form-item>
      </el-form>
      <span slot="reference"
            class="add_a_rule"
            @click="addVisibleClick">+ {{$t('custom.component.gadget.addRule')}}</span>
    </el-popover>
    <draggable v-if="dataInfoCondition.conditionFormat"
               filter=".edititem,.deleteitem"
               chosenClass="chosen"
               forceFallback="true"
               v-model="dataInfoCondition.conditionFormat"
               animation="1000">
      <transition-group>
        <div class="item"
             v-for="(element,index) in dataInfoCondition.conditionFormat"
             :key="element.name">
          <div class="listbox_item"
               style="padding:0 10px">
            <div style="height:45px;line-height:45px;">
              <span style="display:inline-block;overflow:hidden;text-overflow: ellipsis;white-space:nowrap;font-size: 14px;font-weight: 500;color: rgba(0, 0, 0, 0.75);width:250px">{{element.name}}</span>
              <i style="float:right;margin-left:5px"
                 @click="deletelistbox(index)"
                 class="cuifont icon_delete deleteitem"></i>

              <i style="float:right"
                 class="cuifont icon_settings_dev edititem"
                 @click="editVisibleClick(index)"></i>
            </div>
            <el-divider></el-divider>
            <div style="height:49px;display:flex;align-items:center">
              <div style="width:30px;height:30px;margin-right:10px"
                   :style="{backgroundColor:(dataInfoCondition.gadgetType === 'cardnumbergadget' || dataInfoCondition.gadgetType === 'barchartgadget' )? element.textColor : element.backgroundColor}"></div>
              <div style="width:281px;height:30px;font-size: 10px;font-weight: 400;color: rgba(0, 0, 0, 0.75);line-height: 14px;word-break: keep-all;">{{$t('custom.component.gadget.columnText')}}</div>
            </div>
          </div>
        </div>
      </transition-group>
    </draggable>
  </div>
</template>
<script>

import draggable from 'vuedraggable'
import TypeFilter from '@/components/script-render/components/type-filter'
import { filterJson } from '@/components/dashboard-filter/filter-json'
export default {
  components: { draggable, TypeFilter },
  props: {
    dataInfoCondition: {
      type: Object,
      default: () => {
        return {}
      }
    },
    conditionFieldList: {
      type: Array,
      default: () => {
        return []
      }
    },
    ruleList: {
      type: Array,
      default: () => {
        return []
      }
    },
    allValueLists: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      visible: false,
      addRule: {
        name: '',
        dataForm: { 'filters': { 'condition': 'and', 'rules': [] } },
        backgroundColor: '',
        textColor: '',
        editVisible: false,
        entireRow: 'cell'
      },
      addRuleRules: {
        name: [
          { required: true, message: 'please input the name', trigger: 'blur' }
        ]
      },
      scriptItem: filterJson,
      predefineColors: [
        '#ffebe6',
        '#fffae6',
        '#e3fcef',
        '#e6fcff',
        '#deebff',
        '#eae6ff',
        '#e36a78',
        '#f9c97b',
        '#409944'
      ],
      isEditModel: false
    }
  },
  watch: {
    allValueLists: {
      handler (value) {
      },
      deep: true
    }
  },
  created () {
    this.dataInfoCondition.conditionFormat && this.dataInfoCondition.conditionFormat.forEach(item => {
      for (var key in this.addRule) {
        if (!item.hasOwnProperty(key)) {
          item[key] = this.addRule[key]
        }
      }
    })
  },
  methods: {
    addVisibleClick () {
      this.visible = true
      this.isEditModel = false
    },
    editVisibleClick (index) {
      this.addRule = JSON.parse(JSON.stringify(this.dataInfoCondition.conditionFormat[index]))
      this.visible = true
      this.isEditModel = true
      this.editIndex = index
    },
    deletelistbox (index) {
      this.dataInfoCondition.conditionFormat.splice(index, 1)
      this.$forceUpdate()
    },
    add () {
      this.addRule.name = this.addRule.name.replace(/(^\s*)|(\s*$)/g, '')
      this.$refs.newAddRuleForm.validate(isPass => {
        if (isPass) {
          if (!Array.isArray(this.dataInfoCondition.conditionFormat)) {
            this.dataInfoCondition.conditionFormat = []
          }
          this.dataInfoCondition.conditionFormat.push(JSON.parse(JSON.stringify(this.addRule)))
          this.$emit('update:ruleList', this.dataInfoCondition.conditionFormat)
          this.resetForm()
        }
      })
    },
    edit () {
      this.addRule.name = this.addRule.name.replace(/(^\s*)|(\s*$)/g, '')
      this.$refs.newAddRuleForm.validate(isPass => {
        if (isPass) {
          this.dataInfoCondition.conditionFormat[this.editIndex] = JSON.parse(JSON.stringify(this.addRule))
          this.$emit('update:ruleList', this.dataInfoCondition.conditionFormat)
          this.$forceUpdate()
          this.resetForm()
        }
      })
    },
    resetForm () {
      this.$refs.newAddRuleForm.resetFields()
      this.addRule = {
        name: '',
        dataForm: { 'filters': { 'condition': 'and', 'rules': [] } },
        backgroundColor: '',
        textColor: '',
        entireRow: 'cell'
      }
      if (this.$refs.addtypeFilter) {
        this.$refs.addtypeFilter.deletePopClose()
      }
      this.visible = false
    },
    updateData () {

    }
  }
}
</script>
<style lang="scss">
.addrule_el_popover {
  // overflow: auto !important;
  // min-height: 400px;
  // max-height: 460px;
  padding: 20px;
  padding-top: 0;
  .add_title {
    font-size: 18px;
    font-weight: 500;
    color: rgba(0, 0, 0, 0.75);
    line-height: 54px;
    height: 54px;
  }
  .add_close {
    font-size: 16px;
    float: right;
    margin-top: 21px;
    &:hover {
      cursor: pointer;
    }
  }
  .add_name {
    font-size: 14px;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
  }
}
.condition_setting {
  min-height: 200px;
  & > div {
    max-height: 300px;
    overflow-x: hidden;
    overflow-y: scroll;
  }
  .tips {
    font-size: 14px;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.5);
    line-height: 20px;
  }
  .add_a_rule {
    height: 46px;
    width: 100%;
    display: inline-block;
    border-bottom: 1px solid #f0f0f0;
    line-height: 46px;
    padding: 0 0 16px 16px;
    color: #138dc7;
    font-size: 14px;
    &:hover {
      cursor: pointer;
    }
  }
  .e-list-item {
    height: 94px;
    margin-bottom: 10px !important;
    // background-color: #ff0;
  }
  .e-listbox-wrapper {
    background-color: #fff !important;
  }
  .edititem:hover,
  .deleteitem:hover {
    cursor: pointer;
  }
  .chosen {
    border: solid 2px #3089dc !important;
  }
  .item {
    padding: 6px;
    background-color: #fdfdfd;
    border: solid 1px #eee;
    margin-bottom: 10px;
    cursor: move;
    width: 343px;
  }
  .el-divider {
    margin: 0 !important;
  }
}
</style>
